package pl.edu.agh.mguesser.ui.game;

import pl.edu.agh.mguesser.ui.main.*;
import com.google.common.io.Resources;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.annotation.PostConstruct;
import javax.imageio.ImageIO;
import javax.swing.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import pl.edu.agh.mguesser.exception.MusicGuesserRuntimeException;
import pl.edu.agh.mguesser.game.GameFlow;
import pl.edu.agh.mguesser.game.GameFlowException;
import pl.edu.agh.mguesser.ui.AwareOfBecomingActivePanel;
import pl.edu.agh.mguesser.ui.SizeConstants;
import pl.edu.agh.mguesser.ui.Window;
import pl.edu.agh.mguesser.ui.swing.ImagePanel;

/**
 * @author keiran
 */
@Controller
public class GameInProgress extends JPanel implements AwareOfBecomingActivePanel {
    
    @Autowired
    private UserPanel userPanel;
    @Autowired
    private RoundPanel roundPanel;
    @Autowired
    private SongProgressPanel songProgressPanel;
    @Autowired
    private StopPlayingPanel stopPlayingPanel;
    @Autowired
    private SongRevealedPanel songRevealedPanel;
    @Autowired
    private GetAnswerPanel getAnswerPanel;
    @Autowired
    private ScorePanel scorePanel;
    
    @Autowired
    private Window window;
    @Autowired
    private NewGame newGame;
    
    private GameFlow gameFlow;
    
    private JButton backBtn;
    
    @PostConstruct
    public void postConstruct() {
        setLayout(new BorderLayout());
        
        addTopBar();
        addMiddlePanel();
        addButtons();
        
        bindButtons();
    }

    private void addTopBar() {
        try {
            BufferedImage image = ImageIO.read(Resources.getResource("img/top_bar.jpg"));
            
            JPanel panel = new ImagePanel(image);
            Dimension size
                    = new Dimension(SizeConstants.WINDOW_WIDTH, SizeConstants.TOP_BAR_HEIGHT);
            panel.setPreferredSize(size);
            add(panel, BorderLayout.NORTH);
            
        } catch(IOException ex) {
            throw new MusicGuesserRuntimeException(ex);
            
        }
    }

    private void addMiddlePanel() {
        JPanel center = new JPanel(new GridLayout(4, 2));
        
        center.add(userPanel);
        center.add(roundPanel);
        center.add(songProgressPanel);
        center.add(stopPlayingPanel);
        center.add(songRevealedPanel);
        center.add(getAnswerPanel);
        center.add(scorePanel);
        
        add(center, BorderLayout.CENTER);
    }

    private void addButtons() {
        JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        
        backBtn = new JButton("To New Game");
        buttonsPanel.add(backBtn);
        
        add(buttonsPanel, BorderLayout.SOUTH);
    }

    private void bindButtons() {
        backBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                endGame();
            }
        });
    }

    public void setGameFlow(GameFlow gameFlow) {
        this.gameFlow = gameFlow;
    }

    @Override
    public void becameActive() {
        if(gameFlow == null) {
            throw new IllegalStateException();
        }
        
        try {
            gameFlow.play();
            
        } catch (GameFlowException ex) {
            throw new MusicGuesserRuntimeException(ex);
            
        } finally {
            endGame();
            
        }
    }
    
    private void endGame() {
        if(gameFlow != null) {
            gameFlow.donePlaying();
            gameFlow = null;
        }
        
        window.changePanel(newGame);
    }
    
}
